package com.li.arithmetic;

import java.util.Arrays;

public class FourDivided {
//	static int[] number={2,5,1,1,1,1,4,1,7,3,7};
//	static int[] number2={2,5,1,1,1,1,4,1,7,3,7};
//	static int[] number={6,6,3,1,2,9,4,2,2,8,9,9,3};
//	static int[] number2={6,6,3,1,2,9,4,2,2,8,9,9,3};
	static int[] number={1000,999,1,1,500,1000,500,100000,1,1999,12,2000};
	static int[] number2={1000,999,1,1,500,1000,500,100000,1,1999,12,2000};
//	static int[] number={10,2,11,13,1,1,1,1,1};
//	static int[] number2={10,2,11,13,1,1,1,1,1};
	public static void main(String[] args) {
		int[] temp=minSort(number);
		int maxall=0;
		int minall=0;
		for(int i=3;i<temp.length;i++){
			maxall+=temp[i];
		}
		System.out.println("maxall:"+maxall);
		for(int i=0;i<temp.length-3;i++){
			minall+=temp[i];
		}
		System.out.println("minall:"+minall);
		int max=maxall/4;
		int min=minall/4;
		System.out.println("min:"+min+"---max:"+max);
		int[] n=new int[4];
		int[] n1=new int[3];
		int temp2=0;
		for(int i=0,j=0;i<number2.length;i++){
			temp2+=number2[i];
			if(temp2>=min && max>=temp2){
				if((j==0) || (n[j-1]!=0 && n[j-1]==temp2)){
					if(j<3){
						n1[j]=i;
					}
					n[j]=temp2;
					j++;
					i++;
					temp2=0;
				}
			}
			
			if(temp2>max){
				i=-1;
				j=0;
				min+=1;
				if(min>max){
					System.out.println("无法四等分！");
					break;
				}
				temp2=0;
			}
			if(i==(number2.length)){
				if(n[0]==n[1] && n[1]==n[2] &&n[2]==n[3]){
					System.out.println("四等分成功！");
					System.out.println("原数组为:"+Arrays.toString(number2));
					System.out.println("划分下标分别为:"+Arrays.toString(n1));
					System.out.println("四等分的值为:"+Arrays.toString(n));
				}
				
			}
		}
	}
	
	public static int[] minSort(int[] number){
		Arrays.sort(number);
		return number;
	}
}
